﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace Notoric.ViewModel.Converters
{
    /// <summary>
    /// Converts between the structures used in the model (e.g. <see cref="Notoric.Model.Elements.Point"/> or
    /// <see cref="Notoric.Model.Elements.Size"/>) and their counter-parts in Windows Presentation Foundation.
    /// </summary>
    public class StructuresConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>A converted value. If the method returns <see langword="null"/>, the valid null value is used.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;
            if (targetType == null)
                throw new ArgumentNullException("targetType");

            var lengthConverter = new LengthConverter();

            if (value is Model.Elements.Thickness)
            {
                if (targetType.IsAssignableFrom(typeof(Thickness)))
                {
                    Model.Elements.Thickness thickness = (Model.Elements.Thickness)value;

                    return new Thickness(
                        (double)lengthConverter.Convert(thickness.Left, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.Convert(thickness.Top, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.Convert(thickness.Right, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.Convert(thickness.Bottom, typeof(double), null, CultureInfo.CurrentCulture)
                        );
                }
            }
            else if (value is Model.Elements.Size)
            {
                if (targetType.IsAssignableFrom(typeof(Size)))
                {
                    Model.Elements.Size size = (Model.Elements.Size)value;

                    return new Size(
                        (double)lengthConverter.Convert(size.Width, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.Convert(size.Height, typeof(double), null, CultureInfo.CurrentCulture)
                        );
                }
            }
            else if (value is Model.Elements.Point)
            {
                if (targetType.IsAssignableFrom(typeof(Point)))
                {
                    Model.Elements.Point point = (Model.Elements.Point)value;

                    return new Point(
                        (double)lengthConverter.Convert(point.X, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.Convert(point.Y, typeof(double), null, CultureInfo.CurrentCulture)
                        );
                }
            }
            else if (value is Model.Elements.Color)
            {
                if (targetType.IsAssignableFrom(typeof(Color)))
                {
                    var color = (Model.Elements.Color)value;

                    return new Color()
                    {
                        A = color.Alpha,
                        B = color.Blue,
                        G = color.Green,
                        R = color.Red
                    };
                }
            }

            throw new InvalidOperationException(String.Format("Cannot convert from {0} to {1}!",
                        value.GetType().Name, targetType.Name)
                        );
        }

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>A converted value. If the method returns <see langword="null"/>, the valid null value is used.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;
            if (targetType == null)
                throw new ArgumentNullException("targetType");

            var lengthConverter = new LengthConverter();

            if (value is Thickness)
            {
                if (targetType.IsAssignableFrom(typeof(Model.Elements.Thickness)))
                {
                    Thickness thickness = (Thickness)value;

                    return new Model.Elements.Thickness(
                        (double)lengthConverter.ConvertBack(thickness.Left, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.ConvertBack(thickness.Top, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.ConvertBack(thickness.Right, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.ConvertBack(thickness.Bottom, typeof(double), null, CultureInfo.CurrentCulture)
                        );
                }
            }
            else if (value is Size)
            {
                if (targetType.IsAssignableFrom(typeof(Model.Elements.Size)))
                {
                    Size size = (Size)value;

                    return new Model.Elements.Size(
                        (double)lengthConverter.ConvertBack(size.Width, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.ConvertBack(size.Height, typeof(double), null, CultureInfo.CurrentCulture)
                        );
                }
            }
            else if (value is Point)
            {
                if (targetType.IsAssignableFrom(typeof(Model.Elements.Point)))
                {
                    Point point = (Point)value;

                    return new Model.Elements.Point(
                        (double)lengthConverter.ConvertBack(point.X, typeof(double), null, CultureInfo.CurrentCulture),
                        (double)lengthConverter.ConvertBack(point.Y, typeof(double), null, CultureInfo.CurrentCulture)
                        );
                }
            }
            else if (value is Color)
            {
                if (targetType.IsAssignableFrom(typeof(Model.Elements.Color)))
                {
                    var color = (Color)value;

                    return new Model.Elements.Color(
                        color.R, color.G, color.B, color.A
                        );
                }
            }

            throw new InvalidOperationException(String.Format("Cannot convert from {0} to {1}!",
                        value.GetType().Name, targetType.Name)
                        );
        }

        #endregion
    }
}
